package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.YogaShopBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.YogaShopQTO;
import com.yoga.dao.dao.YogaShopDao;
import com.yoga.dao.domain.YogaShopDO;
import com.yoga.manager.admin.AdminYogaShopManager;
import com.yoga.manager.beanUtil.YogaShopBeanUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/9/29.
 */
@Component
public class AdminYogaShopManagerImpl implements AdminYogaShopManager {

    @Resource
    private YogaShopDao yogaShopDao;

    @Override
    public YogaShopBO addData(YogaShopBO yogaShopBO) throws AppException {
        if (yogaShopBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaShopBO.getYogaAdminId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (StringUtils.isEmpty(yogaShopBO.getShopName()) || StringUtils.isEmpty(yogaShopBO.getShopAddress())) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        YogaShopDO yogaShopDO = YogaShopBeanUtil.yogaShopBOToDO(yogaShopBO);

        int num = yogaShopDao.insert(yogaShopDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }

        return YogaShopBeanUtil.yogaShopDOToBO(yogaShopDao.findById(yogaShopDO.getId()));
    }

    @Override
    public Boolean updateData(YogaShopBO yogaShopBO) throws AppException {
        if (yogaShopBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaShopBO.getYogaShopId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        int num = yogaShopDao.updateById(YogaShopBeanUtil.yogaShopBOToDO(yogaShopBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }


        return true;
    }

    @Override
    public Boolean deleteData(Long yogaShopId) throws AppException {
        if (yogaShopId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        YogaShopDO update = new YogaShopDO();
        update.setId(yogaShopId);
        update.setDeleteMark(1);
        int num = yogaShopDao.updateById(update);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }


        return true;
    }

    @Override
    public YogaShopBO findById(Long yogaShopId) throws AppException {
        if (yogaShopId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }

        return YogaShopBeanUtil.yogaShopDOToBO(yogaShopDao.findById(yogaShopId));
    }

    @Override
    public PageBO<YogaShopBO> findForPage(YogaShopQTO yogaShopQTO) throws AppException {
        PageBO<YogaShopBO> pageBO = new PageBO<>();
        if (yogaShopQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaShopQTO.getYogaAdminId() == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        pageBO.setQuery(yogaShopQTO);
        Integer count = yogaShopDao.findForPageCount(yogaShopQTO);
        if (count == null || count <= 0) {
            return pageBO;
        }
        List<YogaShopBO> boList = YogaShopBeanUtil.doList2BOList(yogaShopDao.findForPage(yogaShopQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(boList);

        return pageBO;
    }
}
